﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;

namespace JsonToXML
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private string getChoice(string str)
        {
            char[] list = str.ToCharArray();
            string result = "";
            foreach (var item in list)
            {
                if (item.ToString().ToUpper() == "A")
                {
                    result += "1";
                }
                if (item.ToString().ToUpper() == "B")
                {
                    result += "2";
                }
                if (item.ToString().ToUpper() == "C")
                {
                    result += "3";
                }
                if (item.ToString().ToUpper() == "D")
                {
                    result += "4";
                }
            }
            return result;
        }

        private string xml2Json()
        {
            string xmlString = System.IO.File.ReadAllText("DataXMLFile.xml",Encoding.GetEncoding("gb2312"));
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xmlString);
            string jsonText = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc);
            return jsonText;
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            string jsonString = System.IO.File.ReadAllText("zz.json");
            var jsonList = JsonConvert.DeserializeObject<JsonRootobject>(jsonString);
            
            var xmlList = JsonConvert.DeserializeObject<XmlRootobject>(xml2Json());

            XDocument xdoc = new XDocument();

            XElement subjects = new XElement("subjects");

            for (int i = 0; i < jsonList.rows.Length; i++)
            {
                var row = jsonList.rows[i];
                XElement topic = new XElement("topic");

                topic.Add(new XElement("no", i + 1));
                topic.Add(new XElement("title", row.qb_content.Replace("\n","")));
                topic.Add(new XElement("image", row.qb_address.Replace("/", ".")));
                if (row.TextType == 2 || row.TextType == 3)
                {
                    topic.Add(new XElement("choiceA", row.qb_candidateA == null ? "":row.qb_candidateA.Replace("\n", "")));
                    topic.Add(new XElement("choiceB", row.qb_candidateB == null ? "" : row.qb_candidateB.Replace("\n", "")));
                    topic.Add(new XElement("choiceC", row.qb_candidateC == null ? "" : row.qb_candidateC.Replace("\n", "")));
                    topic.Add(new XElement("choiceD", row.qb_candidateD == null ? "" : row.qb_candidateD.Replace("\n", "")));
                    topic.Add(new XElement("key", getChoice(row.qb_answer)));
                    topic.Add(new XElement("choiceP", ""));

                    if (row.TextType == 2)
                    {
                        topic.Add(new XElement("type", "单选题"));
                        
                    }
                    else if (row.TextType == 3)
                    {
                        topic.Add(new XElement("type", "多选题"));
                        
                    }


                }
                else if (row.TextType == 1)
                {
                    topic.Add(new XElement("choiceA", ""));
                    topic.Add(new XElement("choiceB", ""));
                    topic.Add(new XElement("choiceC", ""));
                    topic.Add(new XElement("choiceD", ""));
                    topic.Add(new XElement("key", ""));
                    topic.Add(new XElement("choiceP", getChoice(row.qb_answer)));
                    topic.Add(new XElement("type", "判断题"));
                    
                }
                string content = row.qb_content.Split('？')[0];
                var xmlObj = xmlList.subjects.topic.Where(pp => pp.title.StartsWith(content)).ToList();
                if (xmlObj.Count > 0)
                {
                    topic.Add(new XElement("mokuai", xmlObj == null ? "" : xmlObj.First().mokuai));
                    topic.Add(new XElement("zhangjie", xmlObj == null ? "" : xmlObj.First().zhangjie));
                }
                else
                {
                    topic.Add(new XElement("mokuai", "道路货物运输驾驶员"));
                    topic.Add(new XElement("zhangjie", "7.其它"));

                    //topic.Add(new XElement("teshu", "没找到"));
                }

                if (row.TextType == 1 || row.TextType == 2)
                {
                    topic.Add(new XElement("fenzhi", "1"));
                }
                else if (row.TextType == 3)
                {
                    topic.Add(new XElement("fenzhi", "2"));
                }

                subjects.Add(topic);
            }

            xdoc.Add(subjects);

            xdoc.Save("topic.xml");
            
            MessageBox.Show("生成成功");
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            string msg = string.Empty;
            var jieshi = NPOIClass.ExcelToDataSet("jieshi.xlsx",out msg);
            for (int i = 1; i < jieshi.Tables[0].Rows.Count; i++)
            {
                var item = jieshi.Tables[0].Rows[i];
                string title = item[0].ToString();
                string js = item[11].ToString();
                if (js != "123")
                {
                    UpdateXML(title, js);
                }
                
            }
        }

        private void UpdateXML(string title,string jieshi)
        {
            string path = "DataXMLFile.xml";
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path);
            var elements = (XmlNodeList)xmlDoc.SelectNodes("subjects/topic");

            for (int i = 0; i < elements.Count; i++)
            {
                var item = elements[i];

                if (item.ChildNodes[1].InnerText == title)
                {
                    item.ChildNodes[13].InnerText = jieshi;
                    break;
                }

            }

            xmlDoc.Save(path);
        }
    }
}
